import 'package:flutter/material.dart';

///***********************************************
///文件描述：弹出指定Y轴下、横行充满的弹窗，并从Y轴位置执行平移动画
///创建人：dgg108698
///创建时间：2020/10/29
///修改人：
///修改时间：
///修改备注：
///***********************************************

class _DialogRoute<T> extends PopupRoute<T> {
  _DialogRoute({
    @required this.builder,
    @required this.top,
    String barrierLabel,
    RouteSettings settings,
  })  : _barrierLabel = barrierLabel,
        super(settings: settings);

  final WidgetBuilder builder;

  final double top;

  @override
  bool get barrierDismissible => true;

  @override
  String get barrierLabel => _barrierLabel;
  final String _barrierLabel;

  @override
  Color get barrierColor => Colors.transparent;

  @override
  Duration get transitionDuration => Duration(milliseconds: 380);

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return Material(
      type: MaterialType.transparency,
      child: Padding(
        padding: EdgeInsets.only(top: top),
        child: Stack(
          children: [
            GestureDetector(
              onTap: () => Navigator.pop(context),
              child: _Color(
                position: animation.drive(_colorTween.chain(_fastTween)),
              ),
            ),
            CustomSingleChildLayout(
              delegate: _ChildLayoutDelegate(),
              child: ClipRect(
                clipBehavior: Clip.hardEdge,
                child: SlideTransition(
                  position: animation.drive(_offsetTween.chain(_fastTween)),
                  child: MediaQuery.removePadding(
                    removeTop: true,
                    removeBottom: true,
                    context: context,
                    child: builder(context),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  final Tween<Offset> _offsetTween = Tween<Offset>(
    begin: Offset(0.0, -1),
    end: Offset(0.0, 0),
  );
  final ColorTween _colorTween =
      ColorTween(begin: Colors.transparent, end: Color(0x88000000));

  final Animatable<double> _fastTween = CurveTween(curve: Curves.ease);
}

class _Color extends AnimatedWidget {
  _Color({
    @required Animation<Color> position,
  }) : super(listenable: position);

  Animation<Color> get color => listenable as Animation<Color>;

  @override
  Widget build(BuildContext context) {
    Color value = color.value;
    return Container(
      color: value,
    );
  }
}

class _ChildLayoutDelegate extends SingleChildLayoutDelegate {
  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return constraints.copyWith(
      minWidth: constraints.maxWidth,
      maxWidth: constraints.maxWidth,
      minHeight: 0.0,
      maxHeight: constraints.maxHeight,
    );
  }

  @override
  bool shouldRelayout(covariant _ChildLayoutDelegate oldDelegate) {
    return false;
  }
}

Future<T> cpsShowDropDownDialog<T>(
    {@required BuildContext context,
    @required WidgetBuilder builder,
    @required double yAxis}) {
  return Navigator.push(context, _DialogRoute(builder: builder, top: yAxis));
}
